#include "../../source/server.hpp"

// void HandleClose(Channel *channel)
// {
//     logMessage(DEBUG,"close fd:%d",channel->Fd());
//     channel->Remove();//移除监控
//     delete channel;
// }

// void HandleRead(Channel *channel)
// {
//     int fd = channel->Fd();
//     char buf[1024] = {0};
//     int ret = recv(fd,buf,sizeof(buf) - 1,0);
//     if(ret <= 0)
//         return HandleClose(channel);
//     logMessage(DEBUG,"server_test->HandleRead recv: %s",buf);
//     channel->EnableWrite();
// }
// void HandleWrite(Channel* channel)
// {
//     int fd = channel->Fd();
//     // std::string str = "一会要下雨了!";
//     // int ret = send(fd,str.c_str(),str.size(),0);
//     const char* data = "一会要下雨了";
//     int ret = send(fd,data,strlen(data),0);
//     if(ret < 0)
//         return HandleClose(channel);
//     channel->DisableWrite();
// }
// void HandleError(Channel *Channel)
// {
//     return HandleClose(Channel);//直接释放
// }
// void HandleEvent(EventLoop *loop,Channel *channel,uint64_t timefd)
// {
//     loop->TimerRefresh(timefd);
// }
// void Acceptor(EventLoop *loop,Channel *lst_channel)
// {
//     int fd = lst_channel->Fd();
//     int newfd = accept(fd,nullptr,nullptr);
//     if(newfd < 0)
//         return;
//     uint64_t timerid = rand() % 10000;
//     Channel *channel = new Channel(loop,newfd);
//     channel->SetReadCallback(std::bind(HandleRead,channel));
//     channel->SetWriteCallback(std::bind(HandleWrite,channel));
//     channel->SetErrorCallback(std::bind(HandleError,channel));
//     channel->SetEventCallback(std::bind(HandleEvent,loop,channel,timerid));
//     channel->SetCloseCallback(std::bind(HandleClose,channel));
//     loop->TimerAdd(timerid,10,std::bind(HandleClose,channel));
//     channel->EnableRead();
// }

/**
 * 对于channel和EventLoop的测试
*/
// void HandleClose(Channel *channel)
// {
//     logMessage(DEBUG,"Refreshed discovery fd:%d Timed out, release has occurred",channel->Fd());
//     channel->Remove();//移除监控
//     delete channel;
// }

// void HandleRead(Channel *channel)
// {
//     int fd = channel->Fd();
//     char buf[1024] = {0};
//     int ret = recv(fd,buf,sizeof(buf) - 1,0);
//     if(ret <= 0)
//     {
//         channel->Remove();
//         return HandleClose(channel);
//     }
//     logMessage(DEBUG,"server_test->HandleRead recv: %s",buf);
//     channel->EnableWrite();    
    
    
// }
// void HandleWrite(Channel* channel)
// {
//     int fd = channel->Fd();
//     const char* data = "一会要下雨了";
//     int ret = send(fd,data,strlen(data),0);
//     if(ret < 0)
//         return HandleClose(channel);
//     channel->DisableWrite();
// }
// void HandleError(Channel *Channel)
// {
//     return HandleClose(Channel);//直接释放
// }
// void HandleEvent(EventLoop *loop,Channel *channel,uint64_t timerid)
// {
//     loop->TimerRefresh(timerid);
//     logMessage(DEBUG,"handleEvent new one");
// }
// void Acceptor_channel(EventLoop *loop,Channel *lst_channel)
// {
//     int fd = lst_channel->Fd();
//     int newfd = accept(fd,nullptr,nullptr);
//     if(newfd < 0)
//         return;
//     uint64_t timerid = rand() % 10000;
//     Channel *channel = new Channel(loop,newfd);
//     channel->SetReadCallback(std::bind(HandleRead,channel));
//     channel->SetWriteCallback(std::bind(HandleWrite,channel));
//     channel->SetErrorCallback(std::bind(HandleError,channel));
//     channel->SetCloseCallback(std::bind(HandleClose,channel));
//     channel->SetEventCallback(std::bind(HandleEvent,loop,channel,timerid));
//     loop->TimerAdd(timerid,10,std::bind(HandleClose,channel));//添加定时任务
//     channel->EnableRead();//启动可读事件监控
// }
// int main()
// {
//     srand(time(NULL));
//     EventLoop loop;
//     Socket lst_sock;
//     lst_sock.CreateServer(8500);
//     Channel channel(&loop,lst_sock.Fd());
//     channel.SetReadCallback(std::bind(Acceptor_channel,&loop,&channel));
//     channel.EnableRead();
//     while(1)
//     {
//         loop.Start();
//     }
//     lst_sock.Close();
//     return 0;
// }

/**
 * 对于添加Any和Connection之后的测试
*/
// std::unordered_map<uint64_t,PtrConnection> _conns;
// uint64_t conn_id = 0;
// void ConnectionDestory(const PtrConnection &conn)
// {
//     logMessage(DEBUG,"remove a connection:%d",conn->Id());
//     _conns.erase(conn->Id());
// }
// void OnConnected(const PtrConnection &conn)
// {
//     logMessage(DEBUG,"new connection: %p",conn.get());
// }
// void OnMessage(const PtrConnection &conn,Buffer* buf)
// {
//     logMessage(DEBUG,"recv a message:%s",buf->ReadPosition());
//     buf->MoveReadOffest(buf->ReadAbleSize());
//     std::string str = "I sent you a message";
//     conn->Send(str.c_str(),str.size());
//     conn->ShutDown();
// }

// void Acceptor_connection(EventLoop *loop,Channel *lst_channel)
// {
//     int fd = lst_channel->Fd();
//     int newfd = accept(fd,nullptr,nullptr);
//     if(newfd < 0)
//         return;
//     conn_id++;
//     PtrConnection conn(new Connection(loop,conn_id,newfd));
//     conn->SetMessageCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
//     conn->SetSrvClosedCallback(std::bind(ConnectionDestory,std::placeholders::_1));
//     conn->SetConnectedCallback(std::bind(OnConnected,std::placeholders::_1));
//     conn->EnableInactiveRelease(10);//启动超时销毁
//     conn->Established();//初始化
//     _conns.insert(std::make_pair(conn_id,conn)); 
// }
// //对于Connection的测试
// int main()
// {
//     srand(time(NULL));
//     EventLoop loop;
//     Socket lst_sock;
//     lst_sock.CreateServer(8500);
//     Channel channel(&loop,lst_sock.Fd());
//     channel.SetReadCallback(std::bind(Acceptor_connection,&loop,&channel));
//     channel.EnableRead();
//     while(1)
//     {
//         loop.Start();
//     }
//     lst_sock.Close();
//     return 0;
// }

/* Accept模块的测试*/
// std::unordered_map<uint64_t,PtrConnection> _conns;
// uint64_t conn_id = 0;
// EventLoop loop;
// void ConnectionDestory(const PtrConnection &conn)
// {
//     logMessage(DEBUG,"remove a connection:%d",conn->Id());
//     _conns.erase(conn->Id());
// }
// void OnConnected(const PtrConnection &conn)
// {
//     logMessage(DEBUG,"new connection: %p",conn.get());
// }
// void OnMessage(const PtrConnection &conn,Buffer* buf)
// {
//     logMessage(DEBUG,"recv a message:%s",buf->ReadPosition());
//     buf->MoveReadOffest(buf->ReadAbleSize());
//     std::string str = "I sent you a message";
//     conn->Send(str.c_str(),str.size());
//     conn->ShutDown();
// }

// void Acceptor_connection(int fd)
// {
//     conn_id++;
//     PtrConnection conn(new Connection(&loop,conn_id,fd));
//     conn->SetMessageCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
//     conn->SetSrvClosedCallback(std::bind(ConnectionDestory,std::placeholders::_1));
//     conn->SetConnectedCallback(std::bind(OnConnected,std::placeholders::_1));
//     conn->EnableInactiveRelease(10);//启动超时销毁
//     conn->Established();//初始化
//     _conns.insert(std::make_pair(conn_id,conn)); 
// }

// int main()
// {
//     srand(time(NULL));
//     Acceptor accept(&loop,8500);
//     accept.SetAcceptCallBack(std::bind(Acceptor_connection,std::placeholders::_1));
//     accept.Listen();
//     while(1)
//     {
//         loop.Start();
//     }
//     return 0;
// }

/* LoopThread模块测试*/
// std::unordered_map<uint64_t,PtrConnection> _conns;
// uint64_t conn_id = 0;
// EventLoop base_loop;
// std::vector<LoopThread> threads(2);
// int next_loop = 0;
// void ConnectionDestory(const PtrConnection &conn)
// {
//     logMessage(DEBUG,"remove a connection:%d",conn->Id());
//     _conns.erase(conn->Id());
// }
// void OnConnected(const PtrConnection &conn)
// {
//     logMessage(DEBUG,"new connection: %p",conn.get());
// }
// void OnMessage(const PtrConnection &conn,Buffer* buf)
// {
//     logMessage(DEBUG,"recv a message:%s",buf->ReadPosition());
//     buf->MoveReadOffest(buf->ReadAbleSize());
//     std::string str = "I sent you a message";
//     conn->Send(str.c_str(),str.size());
//     conn->ShutDown();
// }

// void Acceptor_connection(int fd)
// {
//     conn_id++;
//     next_loop = (next_loop + 1) % 2;
//     PtrConnection conn(new Connection(threads[next_loop].GetLoop(),conn_id,fd));
//     conn->SetMessageCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
//     conn->SetSrvClosedCallback(std::bind(ConnectionDestory,std::placeholders::_1));
//     conn->SetConnectedCallback(std::bind(OnConnected,std::placeholders::_1));
//     conn->EnableInactiveRelease(10);//启动超时销毁
//     conn->Established();//初始化
//     _conns.insert(std::make_pair(conn_id,conn)); 
//     std::cout << pthread_self() << std::endl;//(void*)
// }

// int main()
// {
//     srand(time(NULL));
//     Acceptor accept(&base_loop,8500);
//     accept.SetAcceptCallBack(std::bind(Acceptor_connection,std::placeholders::_1));
//     accept.Listen();
//     while(1)
//     {
//         base_loop.Start();
//     }
//     return 0;
// }

/* LoopThreadPoll模块测试*/
std::unordered_map<uint64_t,PtrConnection> _conns;
uint64_t conn_id = 0;
EventLoop base_loop;
LoopThreadPool *loop_poll;
int next_loop = 0;
void ConnectionDestory(const PtrConnection &conn)
{
    logMessage(DEBUG,"remove a connection:%d",conn->Id());
    _conns.erase(conn->Id());
}
void OnConnected(const PtrConnection &conn)
{
    logMessage(DEBUG,"new connection: %p",conn.get());
}
void OnMessage(const PtrConnection &conn,Buffer* buf)
{
    logMessage(DEBUG,"recv a message:%s",buf->ReadPosition());
    buf->MoveReadOffest(buf->ReadAbleSize());
    std::string str = "I sent you a message";
    conn->Send(str.c_str(),str.size());
    conn->ShutDown();
}

void Acceptor_connection(int fd)
{
    conn_id++;
    PtrConnection conn(new Connection(loop_poll->NextLoop(),conn_id,fd));
    conn->SetMessageCallback(std::bind(OnMessage,std::placeholders::_1,std::placeholders::_2));
    conn->SetSrvClosedCallback(std::bind(ConnectionDestory,std::placeholders::_1));
    conn->SetConnectedCallback(std::bind(OnConnected,std::placeholders::_1));
    conn->EnableInactiveRelease(10);//启动超时销毁
    conn->Established();//初始化
    _conns.insert(std::make_pair(conn_id,conn)); 
    std::cout << pthread_self() << std::endl;//(void*)
}

int main()
{
    loop_poll = new LoopThreadPool(&base_loop);
    loop_poll->SetThreadPoolCount(2);
    loop_poll->Create();
    Acceptor accept(&base_loop,8500);
    accept.SetAcceptCallBack(std::bind(Acceptor_connection,std::placeholders::_1));
    accept.Listen();
    base_loop.Start();
    return 0;
}